/**
 * 每日签到
 */

import { BaseConfig } from "../base/BaseConfig";
import USDate from "../comm/USDate";
import Cfgs from "../configs/Cfgs";
import LocalValues, { LocalKey } from "../data/LocalValues";
// import HallCtrl from "./HallCtrl";
// import { PassCtrl } from "./PassCtrl";

class DailyCtrl {
    private _data: IDailyData;

    public init() {
        // 从local读data
        this._data = LocalValues.getObject(LocalKey.daily);
        if (!this._data) {
            this._data = {
                lastDayIdx: -1,
                lastCount: 0,
                popDayIdx: -1,
            };
        }
    }
    public isPop() {
        const bPop = this._data.lastDayIdx != USDate.dayIdx();
        if (bPop) {
            this._data.popDayIdx = USDate.dayIdx();
            LocalValues.setObject(LocalKey.daily, this._data);
            // PassCtrl.instance.onTaskLogin();
        }
        return bPop;
    }
    /**活动状态 0未开启|等级未达到 1可签到 2已签到 */
    public getState() {
        if (this.unlockDayIdx < 0) {
            return 0; // 活动未开启
        }
        if (this.lastDayIdx == USDate.dayIdx()) {
            return 2; // 已签到
        }
        return 1; // 可签到
    }

    // 三日签到活动结束后，作为开启时间
    public getUnlockDayIdx() {
        // return HallCtrl.getActSigninStartDayIdx() - 3;
        return 0;
    }
    /**今天是活动开启的第几天，小于0表示活动未开启 */
    public get unlockDayIdx() {
        // const newbeeIdx = USDate.dayIdx() - this.getUnlockDayIdx();
        // return newbeeIdx < 0 ? 0 : newbeeIdx;
        return this.getUnlockDayIdx();
    }

    public get lastDayIdx() {
        return this._data.lastDayIdx;
    }

    public get lastCount() {
        return this._data.lastCount % 7;
    }

    // 今天是周签的第几天
    public getTodayIdx() {
        if (this.unlockDayIdx < 0) {
            return -1; // 活动未开启
        }
        const lastDayIdx = this.lastDayIdx;
        if (lastDayIdx < 0) {
            return 0; // 未签到过, 默认从第一天开始
        }
        const todayIdx = USDate.dayIdx();
        if (lastDayIdx == todayIdx) {
            return (this.lastCount % 7) - 1; // 今天已经签到过
        } else if (todayIdx - lastDayIdx == 1) {
            return this.lastCount; // 今天还没签到
        } else {
            this._data.lastCount = 0;
            return 0; // 连续签到中断，从第一天开始
        }
    }

    public get cfg() {
        // return Cfgs.DailyGifts;
        return new BaseConfig<any>();
    }

    public get datas() {
        return this.cfg.getDatas();
    }

    public receive(bAd: boolean) {
        console.log('### DailyCtrl receive', bAd);
        const state = this.getState();
        if (state !== 1) {
            return; // 活动未开启或已签到
        }
        const todayIdx = this.getTodayIdx();
        console.log('### DailyCtrl receive todayIdx', todayIdx);
        if (todayIdx < 0) {
            return; // 活动未开启
        }
        const item = this.datas[todayIdx];
        console.log('### DailyCtrl receive item', item);
        if (!item) {
            return; // 签到数据异常
        }
        const commonReward = JSON.parse(JSON.stringify(item.commonReward));
        const rewards = [...commonReward];
        if (bAd) {
            for (let i = 0; i < item.adReward - 1; i++) {
                rewards.push(...commonReward);
            }
        }
        // HallCtrl.addRewardToQueue(rewards, 5, false);
        this._data.lastDayIdx = USDate.dayIdx();
        this._data.lastCount++;
        LocalValues.setObject(LocalKey.daily, this._data);
        return rewards;
    }
    public getTomorrowCountdown() {
        return USDate.DAY_MILLISECONDS - USDate.todayNow();
    }

    public isTodaySigned(): boolean {
        return this._data.lastDayIdx === USDate.dayIdx();
    }

}

export interface IDailyData {
    lastDayIdx: number;
    lastCount: number;
    popDayIdx: number;
}

export default new DailyCtrl();